﻿using System;
using Csla.Xaml;
using Accounting.Library;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;



namespace csla4Skeleton.UI.SL.NghiepVu.NganHang.NH_GiayCo
{
    public class NH_GiayCo_ViewModel : ViewModel<Accounting.Library.NH_GiayCo>
    {
        #region Event Declare
        public delegate void ChildList_AddedNew_EventHandler(object sender, ChildListAddedNewArgs e);
       
        public delegate void HangHoaIdChanged_EventHandler(object sender, HangHoaIdChangedEventArgs e);
        
        public delegate void ThueSuatIdChanged_EventHandler(object sender, ThueSuatIdChangedEventArgs e);

        public event ChildList_AddedNew_EventHandler ChildList_AddedNew;

      
 
        public event EventHandler TCHangHoaChanged;
        public event EventHandler LoaiTienTeChanged;
       
       
        public event HangHoaIdChanged_EventHandler hangHoaIdChanged;
       
        public event ThueSuatIdChanged_EventHandler ThueSuatIdChanged;  
        #endregion

        #region Refresh Model
        public NH_GiayCo_ViewModel()
        {
            BeginRefresh("NewNH_GiayCo");
            
        }

        public NH_GiayCo_ViewModel(int Khoa)
        {
            BeginRefresh("GetNH_GiayCo", Khoa);
           
        }

        public void NewNH_GiayCoViewModel()
        {
            BeginRefresh("NewNH_GiayCo");
            
        }

        public void GetNH_GiayCoViewModel(int Khoa)
        {
            BeginRefresh("GetNH_GiayCo", Khoa);
        }

       
        public void SaveAndNew()
        {
            BeginSave();
            NewNH_GiayCoViewModel();
        }
        protected override void OnRefreshed()
        {
            base.OnRefreshed();
            if (Model != null)
            {
                Model.NH_GiayCo_ChiTiets.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<NH_GiayCo_ChiTietChild>>(NH_GiayCo_ChiTiets_AddedNew);
                Model.NH_GiayCo_Thues.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<NH_GiayCo_ThueChild>>(NH_GiayCo_Thues_AddedNew);
               
                
                
               
                Model.TinhChatHangHoaChanged += new EventHandler(OnTinhChatHangHoaChanged);
                Model.HangHoaIdChanged += new EventHandler<HangHoaIdChangedEventArgs>(OnHangHoaIdChanged);
               
                Model.LoaiTienTeChanged+= new EventHandler(OnLoaiTienTeChanged);
                
                Model.ThueSuatIdChanged += new EventHandler<ThueSuatIdChangedEventArgs>(OnThueSuatIdChanged);
                Model.BusyEvent += new EventHandler<IsBusyEventArgs>(DoChangingBusy);
            }
        }
        #endregion
        
        #region Add NH_GiayCo Details Item
        public void AddNH_GiayCo_ChiTietItem()
        {
            Model.NH_GiayCo_ChiTiets.AddNewItem(Model.Khoa);
        }

        public void AddNH_GiayCo_ThueItem()
        {
            Model.NH_GiayCo_Thues.AddNewItem(Model.Khoa);
        }

        void NH_GiayCo_ChiTiets_AddedNew(object sender, Csla.Core.AddedNewEventArgs<NH_GiayCo_ChiTietChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("NH_GiayCo_ChiTiets"));
        }      

        void NH_GiayCo_Thues_AddedNew(object sender, Csla.Core.AddedNewEventArgs<NH_GiayCo_ThueChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("NH_GiayCo_Thues"));
        }

        
        


        protected virtual void OnChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (ChildList_AddedNew != null)
            {
                //Invokes the delegates.
                ChildList_AddedNew(sender, e);
            }
        }

      
        public void OnHangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {
            if (hangHoaIdChanged != null)
            {
                hangHoaIdChanged(sender, e);
            }

        }
        public void OnTinhChatHangHoaChanged(object sender, EventArgs e)
        {
            if (TCHangHoaChanged != null)
            {
                TCHangHoaChanged(sender, e);
            }

        }
        public void OnLoaiTienTeChanged(object sender, EventArgs e)
        {
            if (LoaiTienTeChanged != null)
            {
                LoaiTienTeChanged(sender, e);
            }
        }

        public void DoChangingBusy(bool busy)
        {
            IsBusy = busy;
        }
        public void DoChangingBusy(object sender, IsBusyEventArgs e)
        {
            IsBusy = e.Busy;
        }

        

        public void OnThueSuatIdChanged(object sender, ThueSuatIdChangedEventArgs e)
        {
            if (ThueSuatIdChanged != null)
            {
                ThueSuatIdChanged(sender, e);
            }

        }
        #endregion

        #region Delete NH_GiayCo Details Item

        public void DeleteNH_GiayCo_ChiTietItem(int Index)
        {
            Model.NH_GiayCo_ChiTiets.DeleteItem(Index);
        }

        public void DeleteNH_GiayCo_ThueItem(int Index)
        {
            Model.NH_GiayCo_Thues.DeleteItem(Index);
        }
       
      
        

        #endregion

        #region Nghiep Vu So
        internal void GhiSo()
        {
            this.Model.SaveMode = BusinessConfiguration.Configuration.GhiSoSaveMode;
            BeginSave();
        }

        internal void BoGhi()
        {
            int loaiChungTuId = BusinessConfiguration.Configuration.LoaiChungTuId_NH_GiayCo;
            int chungTuGocId = Model.Khoa;

            this.IsBusy = true;
            Accounting.Library.GhiSo.BoGhiService.BoGhi(loaiChungTuId, chungTuGocId, (o, ex) =>
            {
                if (ex.Error != null)
                    this.Model.ErrorMessage = ex.Error.Message;
                else
                    this.Model.ErrorMessage = ((Accounting.Library.GhiSo.BoGhiService)ex.Object).ErrorMessage;
                BeginRefresh("GetNH_GiayCo", Model.Khoa);
            });
            this.IsBusy = false;
        }
#endregion
    }
}
